package com.zzy.controller;

import cn.hutool.jwt.JWT;
import com.zzy.bean.User;
import com.zzy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/login")
    public String login(User user, HttpServletResponse response){
        User u=userService.login(user);
        if (u!=null){
            String token= JWT.create().setPayload("id",u.getUid()).setKey("123456".getBytes()).sign();
            Cookie cookie = new Cookie("TOKEN", token);
            cookie.setMaxAge(30*60);
            cookie.setDomain("localhost");
            response.addCookie(cookie);
        }
        return "success";
    }
}
